<?php
    session_start();
    include('sql.php');
    $uid = $_SESSION['id'];
    $time = time();
    $ip = $_SERVER["REMOTE_ADDR"];
    if (isset($_GET['action'])) {
        $action = $_GET['action'];
        if (isset($_SESSION['id'])) {
            switch ($action) {
                case 'loginout':
                    session_destroy();
                    header("Location: /");
                    break;
                case 'upload':
                    $type = $_POST['type'];
                    if (empty($type)) {
                        echo "请选择文件类型";
                    } else {
                    $targetDir = "File_uploads/".$uid."/"; // 图片上传目录
                    if (!is_dir($targetDir)) {
                        mkdir($targetDir, 0777, true);
                    }
                    $newFileName = time() . uniqid() . '.' . pathinfo($_FILES["imageUpload"]["name"], PATHINFO_EXTENSION);
                    $targetFile = $targetDir . $newFileName;
                    $uploadOk = 1;
                    $imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
                    if (file_exists($targetFile)) {
                        echo "文件已经存在。";
                        $uploadOk = 0;
                    }
                    if ($_FILES["imageUpload"]["size"] > 2000000) {
                        echo "您的文件过大。";
                        $uploadOk = 0;
                    }
                    $allowedTypes = array("jpg", "jpeg", "png", "gif" , "pdf");
                    if (!in_array($imageFileType, $allowedTypes)) {
                        echo "只接受 JPG, JPEG, PNG , GIF , PDF 文件。";
                        $uploadOk = 0;
                    }
                    if ($uploadOk == 0) {
                        echo "您的文件没有被上传。";
                    } else {
                        if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $targetFile)) {
                            $files = dirname($_SERVER['PHP_SELF']) . $newFileName;
                            echo $targetDir . $files;
                            $sql = "insert into upload (id,uid,files,type,time) values ('','$uid','$files','$type','$time')";
                            $result = $link->query($sql);
                            $upload = "UPDATE user SET $type = \"$targetFile\" WHERE id = $uid";
                            $result_upload = $link->query($upload);
                        } else {
                            echo "上传您的文件时出现了错误。";
                        }
                    }
                    }
                    break;
                case 'delfiles':
                    $id = $_GET['id'];
                    $files = $_GET['files'];
                    $sql = "DELETE FROM upload WHERE id = $id";
                    $result = $link->query($sql);
                    $directory = "File_uploads/".$uid."/";
                    $fileToDelete = $files;
                    if ($handle = opendir($directory)) {
                        while (false !== ($file = readdir($handle))) {
                            if ($file != "." && $file != "..") {
                            $filePath = $directory . '/' . $file;
                            if ($file == $fileToDelete) {
                                if (unlink($filePath)) {
                                    header("Location: " . $_SERVER["HTTP_REFERER"]);
                                } else {
                                    echo "删除文件 " . $file . " 时出错。";
                                }
                                break;
                            }
                            }
                        }
                        closedir($handle);
                    }
                    break;
                case 'my':
                    $cardz = htmlspecialchars($_POST['cardz']);
                    $cardf = htmlspecialchars($_POST['cardf']);
                    $sqb = htmlspecialchars($_POST['sqb']);
                    $cns = htmlspecialchars($_POST['cns']);
                    $bgd = htmlspecialchars($_POST['bgd']);
                    $cjd = htmlspecialchars($_POST['cjd']);
                    $exnumber = htmlspecialchars($_POST['exnumber']);
                    $photo = htmlspecialchars($_POST['photo']);
                    $address = htmlspecialchars($_POST['address']);
                    $school = htmlspecialchars($_POST['school']);
                    $years = htmlspecialchars($_POST['years']);
                    $bkzy = htmlspecialchars($_POST['bkzy']);
                    $zylb = htmlspecialchars($_POST['zylb']);
                    $xlzs = htmlspecialchars($_POST['xlzs']);
                    $xwzs = htmlspecialchars($_POST['xwzs']);
                    $xlzsphoto = htmlspecialchars($_POST['xlzsphoto']);
                    $xwzsphoto = htmlspecialchars($_POST['xwzsphoto']);
                    if (empty($cardz = $_POST['cardz'])) {
                        echo "请上传身份证正面";
                    } else {
                        if (empty($cardf)) {
                            echo "请上传身份证反面";
                        } else {
                            if (empty($exnumber)) {
                                echo "准考证号不能为空";
                            } else {
                                if (empty($photo)) {
                                    echo "请上传照片";
                                } else {
                                    if (empty($address)) {
                                        echo "请填写邮寄地址";
                                    } else {
                                        if (empty($school)) {
                                            echo "请填写毕业院校";
                                        } else {
                                            if (empty($bkzy)) {
                                                echo "请填写本科专业";
                                            } else {
                                                if (empty($zylb)) {
                                                    echo "请选择本科门类、专业类。";
                                                } else {
                                                    if (empty($years)) {
                                                        echo "请填写毕业年份";
                                                    } else {
                                                        if (empty($xlzs)) {
                                                            echo "请填写学历证书（毕业证书）编号";
                                                        } else {
                                                            if (empty($xwzs)) {
                                                                echo "请填写学位证书编号";
                                                            } else {
                                                                if (empty($xlzsphoto)) {
                                                                    echo "请上传学历证书（毕业证书）";
                                                                } else {
                                                                    if (empty($xwzsphoto)) {
                                                                        echo "请上传学位证书";
                                                                    } else {
                                                                        if (empty($sqb)) {
                                                                            echo "请上传报名申请表";
                                                                        } else {
                                                                            if (empty($cns)) {
                                                                                echo "请上传考生诚信承诺书";
                                                                            } else {
                                                                                if (empty($bgd)) {
                                                                                echo "请上传学历验证报告单";
                                                                            } else {
                                                                                if (empty($cjd)) {
                                                                                    $cjd = "非必要项";
                                                                                } else {
                                                                                    $cjd = htmlspecialchars($_POST['cjd']);
                                                                                }
                                                                                $sql = "UPDATE user SET exnumber = $exnumber , photo = \"$photo\" , cardz = \"$cardz\" , cardf = \"$cardf\" , sqb = \"$sqb\" , cns = \"$cns\" , address = \"$address\" , bgd = \"$bgd\" , cjd = \"$cjd\" , school = \"$school\" , bkzy = \"$bkzy\" , zylb = \"$zylb\" , years = $years , xlzs = $xlzs , xwzs = $xwzs , xlzsphoto = \"$xlzsphoto\" , xwzsphoto = \"$xwzsphoto\" WHERE id = $uid";
                                                                                $result = $link->query($sql); if ($result) { ?>
                                                                                <div style="margin-top: 36px;">保存成功！</div>
                                                                                <?php } else {
                                                                            echo "保存失败";
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                case 'password':
                    $password = md5($_POST['password']);
                    if (empty($password)) {
                        echo "请输入新密码";
                        exit;
                    } else {
                        $sql = "UPDATE user SET password = \"$password\" where id = $uid";
                        $result = $link->query($sql);
                        if ($result) {
                            echo "修改成功！";
                        } else {
                            echo "修改失败！";
                        }
                    }
                    break;
                case 'select':
                    $speciality = $_POST['speciality'];
                    $sql = "UPDATE user SET speciality = \"$speciality\" , first = 0 ,second = 0 where id = $uid";
                    $result = $link->query($sql);
                    if ($sql) {
                    echo "提交成功";
                    } else {
                        echo "提交失败";
                    }
                    break;
            }
        } else { ?>
                <script type="text/javascript">
                    alert("未登入！");
                    window.history.back();
                </script>
                <?php }
        } else { include('top.php'); ?>
<div class="ui_div_border" id="ui_div_border">
        <table class="ui_border ui_state_visible ui_state_focus ui_state_lock" id="alert">
            <tbody>
                <tr>
                    <td class="ui_lt"></td>
                    <td class="ui_t"></td>
                    <td class="ui_rt"></td>
                </tr>
                <tr>
                    <td class="ui_l"></td>
                    <td class="ui_c">
                        <div class="ui_inner">
                            <table class="ui_dialog">
                                <tbody>
                                    <tr>
                                        <td colspan="2">
                                            <div class="ui_title_bar">
                                                <div class="ui_title" unselectable="on" style="cursor: move;">警告</div>
                                                <div class="ui_title_buttons"><a class="ui_close" href="javascript:void(0);" title="关闭(esc键)" style="display: inline-block;">×</a></div>
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="ui_icon" style=""><img src="/static/img/alert.gif" class="ui_icon_bg"></td>
                                        <td class="ui_main" style="width: 200px; height: auto;">
                                            Gti本科第二学士学位招生报名系统是中国乃至全球最牛的第二学士学位招生报名系统。
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="2">
                                            <div class="ui_buttons" style="">
                                                <input type="button" value="确定" class="ui_state_highlight" onclick="history.go(-1);">
                                            </div>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </td>
                    <td class="ui_r"></td>
                </tr>
                <tr>
                    <td class="ui_lb"></td>
                    <td class="ui_b"></td>
                    <td class="ui_rb" style="cursor: auto;"></td>
                </tr>
            </tbody>
        </table>
    </div>
                    <?php include('bottom.php'); } ?>